package xp2p.trashes;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import net.jxta.peer.PeerID;
import net.jxta.socket.JxtaSocket;


public class Client
{
	private ChannelProvider provider;
	private JxtaSocket _socket;
	private boolean closed = false;
	private PeerID id;
	
	public Client(PeerID id)
	{
		this.id = id;
	}
	
	public void connect() throws IOException 
	{
		_socket = provider.getChannel(id);		
	}
	
	public void send(Serializable message) throws IOException, ClassNotFoundException
	{
		OutputStream out = _socket.getOutputStream();
		ObjectOutputStream outobj = new ObjectOutputStream(out);
		outobj.writeObject(message);			
	}	
	
	public boolean wasClosed()
	{
		return closed;
	}

	public void close()
	{
		try
		{
			closed = true;
			_socket.close();			
		} catch (IOException e)
		{			
			e.printStackTrace();
		}
		finally
		{
			_socket = null;
		}
	}
}